JSP Scriptlet, declaration, expression, 지시자, comment

Scriptlet - <% java 코드 %>

  • jsp페이지에서 java언어를 사용하기 위한 요소
  • 거의 모든 java코드를 사용 가능
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    <body>
    <%
    int i = 0;
    while(true){
    i++;
    out.println("2 * " + i + " = " + ( 2*i ) + "<br/>");
    %>

    ========== <br />

    <%
    if(i>=9) break;
    }
    %>
    </body>

declaration - <%! java 코드 %>

  • jsp페이지 내에서 사용되는 변수 또는 메소드를 선언할 때 사용
  • 여기서 선언된 변수 및 메소드는 전역의 의미로 사용
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    <body>
    <%!
    int i = 10;
    String str = "ABCDE";
    %>
    <%!
    public int sum(int a, int b){
    return a+b;
    }
    %>

    <%
    out.println("i = " + i + "<br />");
    out.println("str = " + str + "<br />");
    out.println("sum = " + sum(1,5) + "<br />");
    %>
    </body>

expressions - <%= java 코드 %>

  • jsp페이지 내에서 사용되는 변수의 값 또는 메소드 호출 결과값을 출력하기 위해 사용
  • 결과값은 String타입이며 ;를 사용할 수 없음
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <body>
    <%!
    int i = 10;
    String str = "ABCDE";

    public int sum(int a, int b){
    return a+b;
    }
    %>

    <%= i %> <br/>
    <%= str %> <br/>
    <%= sum(1,5) %> <br/>
    </body>

지시자

  • jsp페이지의 전체적인 속성을 지정할 때 사용합니다.
  • page,include,taglib가 있으며, <%@ 속성 %>형태로 사용

    1
    2
    3
    page : 해당 페이지의 전체적인 속성 지정
    include : 별도의 페이지를 현재 페이지에 삽입
    taglib : 태그라이브러리의 태그 사용

page

  • 페이지 속성을 지정
  • 주로 사용되는 언어 지정 및 import문을 많이 사용

    1
    <%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>

include

  • 현재 페이지내에 다른 페이지를 삽입할 때 사용
  • file속성을 이용

    1
    2
    3
    <h1> include.jsp페이지 입니다. </h1><br />
    <%@ include file = "include01.jsp" %>
    <h1> 다시 include.jsp 페이지 입니다. </h1><br />

comment

  • html 주석 => <!-- comments --> 소스에서 보임
  • jsp 주석 => <%-- comments --> 컴파일 이후에 출력되므로 소스에서 안보임
Comments